package com.sct.content;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SCTSQLiteOpenHelper extends SQLiteOpenHelper {
	public final static int VERSION = 1;
	
	public final static String TN_MONITOR_DATA = "T_MONITORING_DATA";
	public final static String TN_SYSTEM_CONFIG = "T_SYSTEM_CONFIG";
	public final static String TN_COLLECTION_DATA = "T_COLLECTION_DATA";
	
	public final static String DATABASE_NAME = "sct.db";
	public SCTSQLiteOpenHelper(Context context) {
		super(context, DATABASE_NAME, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE if not exists "+TN_SYSTEM_CONFIG+" (paramkey text PRIMARY KEY,paramvalue text);");
		db.execSQL("CREATE TABLE if not exists "+TN_MONITOR_DATA+" (mdid INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, mdnode TEXT NOT NULL, mdtype TEXT NOT NULL, mdintime TEXT NOT NULL, mddata INTEGER NOT NULL);");
		db.execSQL("CREATE TABLE "+TN_COLLECTION_DATA+" ( "
				+ "cid  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
				+ "cnode  INTEGER NOT NULL, "
				+ "cstatus  INTEGER NOT NULL, "
				+ "cvoltage  INTEGER NOT NULL, "
				+ "chumidity  INTEGER NOT NULL, "
				+ "cbrightness  INTEGER NOT NULL, "
				+ "ctemperature  INTEGER NOT NULL, "
				+ "cinteral  INTEGER NOT NULL, "
				+ "csave  INTEGER NOT NULL, "
				+ "csmessage  TEXT NOT NULL, "
				+ "csintime  TEXT NOT NULL, "
				+ "cintime  INTEGER NOT NULL "
				+ ");");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.i("SCTSQLiteOpenHelper", "--------onUpdate Called--------" + oldVersion + "--->" + newVersion);
		db.execSQL("DROP TABLE if exists "+TN_SYSTEM_CONFIG);
		db.execSQL("DROP TABLE if exists "+TN_MONITOR_DATA);
		db.execSQL("DROP TABLE if exists "+TN_COLLECTION_DATA);
		onCreate(db);
	}

}
